﻿package Chapter01;
//將 StepIntoJava.java 內容替換為下列代碼即可運行
//======================================================================
//public class StepIntoJava {
//	public static void main(String[] args) {
//		RockScissorPaper app = new RockScissorPaper();
//		app.run();
//	}	
//}
//======================================================================

public class RockScissorPaper {
	public int getGuess() {
		int tmp = (int)(Math.random() * 3);
		return tmp;
	}
	
	public String showGuess(int guess) {
		if (guess == 0) {
			return "剪刀";
		} else if (guess == 1) {
			return "石頭";
		} else {
			return "布";
		}
	}
	
	public boolean isMyWin(int myGuess, int yourGuess) {
		if (myGuess == 0 && yourGuess == 2) {
			return true;
		} else if (myGuess == 1 && yourGuess == 0) {
			return true;
		} else if (myGuess == 2 && yourGuess == 1) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * @param args
	 */
	public void run() {
		int myWinCount = 0;
		int yourWinCount = 0;
		
		int i;
		for (i = 0; i < 5; i++) {
			int myGuess = getGuess();
			int yourGuess = getGuess();
			System.out.println("我這次出: "+showGuess(myGuess));
			System.out.println("你這次出: "+showGuess(yourGuess));

			if (myGuess == yourGuess){
				System.out.println("我們兩個平手");
			} else if (isMyWin(myGuess, yourGuess)) {
				System.out.println("耶!我贏了"); 
				myWinCount++;
			} else {
				System.out.println("唉呀....輸了.");
				yourWinCount++;
			}
			System.out.println("=======================");
			
			if (myWinCount >= 3) {
				System.out.println("我贏了整個比賽~~~");
				break;
			} else if (yourWinCount >= 3) {
				System.out.println("你贏了整個比賽~~~");
				break;
			}
		}
		if (i >=5) {
			i = 4;
		}
		
		System.out.printf("最後戰績:%dWin %dLose %d平手\n", 
				myWinCount, yourWinCount, 
				i+1-myWinCount-yourWinCount);
	}
}
